package com.black.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.black.system.domain.SysNotice;
import com.black.system.domain.req.system.add.SysNoticeAddReq;
import com.black.system.domain.req.system.edit.SysNoticeEditReq;
import com.black.system.domain.req.system.query.SysNoticeQueryReq;
import com.black.system.domain.resp.system.SysNoticeResp;
import com.black.system.mapper.SysNoticeMapper;
import com.black.system.service.SysNoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author L
 * @description 针对表【sys_notice(通知公告表)】的数据库操作Service实现
 * @createDate 2024-08-19 10:07:23
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice>
        implements SysNoticeService {

    /**
     * 分页查询
     *
     * @param req
     * @return
     */
    @Override
    public PageInfo<SysNoticeResp> paginationQuery(SysNoticeQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * 添加
     *
     * @param req
     * @return
     */
    @Override
    public boolean add(SysNoticeAddReq req) {

        SysNotice newEntity = new SysNotice();
        // 对象拷贝
        BeanUtils.copyProperties(req, newEntity);

//        newEntity.setNoticeContent(req.getNoticeContent().getBytes());

        boolean save = this.save(newEntity);

        return save;
    }

    /**
     * 修改
     *
     * @param req
     * @return
     */
    @Override
    public boolean edit(SysNoticeEditReq req) {
        // 根据主键查询
        SysNotice sysNotice = this.getById(req.getNoticeId());
        // 判断 查找不到数据
        if (Objects.isNull(sysNotice)) {
            throw new RecordNotFoundException();
        }

//        sysNotice.setUpdateBy(null);
//        sysNotice.setUpdateTime(null);

        // 对象拷贝
        BeanUtils.copyProperties(req, sysNotice);

//        sysNotice.setNoticeContent(req.getNoticeContent().getBytes());

        // 更新
        boolean res = this.updateById(sysNotice);
        return res;
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @Override
    public SysNoticeResp findById(Long id) {
        if (Objects.isNull(id)) {
            throw new PrimaryException();
        }
        // 根据主键查询
        SysNotice sysNotice = this.getById(id);
        if (Objects.isNull(sysNotice)) {
            throw new RecordNotFoundException();
        }
        SysNoticeResp resp = new SysNoticeResp();
        BeanUtils.copyProperties(sysNotice, resp);

        resp.setNoticeContent(new String(sysNotice.getNoticeContent()));

        return resp;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }

        for (Long id : ids) {
            SysNotice sysNotice = this.getById(id);
            if (Objects.nonNull(sysNotice)) {
                this.removeById(id);
            }
        }

        return true;
    }
}




